<?
abstract class LoggerStrings
{
    protected $stringsTable;
    protected $cache = array();
    
    protected function ParseUrl($url)
    {
        $arParsed = @parse_url($url);
        if ($arParsed === false)
            return false;
        $schemeStr = 'http';
        if (isset($arParsed['scheme']))
           $schemeStr = strtolower($arParsed['scheme']);

        if (!isset($arParsed['host']))
            return false;
        $serverStr = strtolower($arParsed['host']);
        if (isset($arParsed['port']) && $arParsed['port'] != '')
            $serverStr .= ':'.$arParsed['port'];
        $pathStr = '/';
        if (isset($arParsed['path']))
            $pathStr = $arParsed['path'];
        $queryStr = '';
        if (isset($arParsed['query']))
            $queryStr = $arParsed['query'];
        if (isset($arParsed['fragment']) && $arParsed['fragment'] != '')
            $queryStr .= '#' . $arParsed['fragment'];
        
        return array(
            'scheme'    => $schemeStr,
            'server'    => $serverStr,
            'path'      => $pathStr,
            'query'     => $queryStr
        );
    }

    protected function GetStringId($string, $time)
    {
        if (strlen($string) == 0)
            return 0;

        $md5 = md5($string);

        if (isset($this->cache[$md5]))
            return $this->cache[$md5];

        $md5Escaped = '\'' . mysql_escape_string($md5) . '\'';
        $string = '\'' . mysql_escape_string($string) . '\'';
        $id = false;

        $this->database->Query('LOCK TABLE `[PREF]' . $this->stringsTable . '` WRITE');
        $query = 'SELECT `id` FROM `[PREF]' . $this->stringsTable . '` WHERE `key`=' . $md5Escaped;
        $result = $this->database->Query($query);
        if ($result && ($row = $this->database->FetchAssoc($result)))
            $id = $row['id'];

        $time = '\'' . mysql_escape_string($time) . '\'';
        if ($id)
        {
            $query = 'UPDATE `[PREF]' . $this->stringsTable . '` SET `last`=' . $time . ' WHERE `id`=' . $id;
            $result = $this->database->Query($query);
            if (!$result)
                trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);
        }
        else
        {
            $query = 'INSERT INTO `[PREF]' . $this->stringsTable . '` (`last`, `key`, `string`) ';
            $query .= 'VALUES(' . $time . ', ' . $md5Escaped . ', ' . $string . ')';
            $result = $this->database->Query($query);
            if (!$result)
                trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);
            $id = $this->database->InsertId();
        }
        $this->database->Query('UNLOCK TABLES');

        if (count($this->cache) < 15000)
            $this->cache[$md5] = $id;

        return $id;
    }

    protected function PurgeStrings($date)
    {
        $DeleteSQL = 'DELETE FROM `[PREF]'.$this->stringsTable.'` WHERE `last`<'.$date;
        $this->database->Query($DeleteSQL);
        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]'.$this->stringsTable.'`';
        $this->database->Query($OptimizeSQL);
    }
}